package com.gxar.quick.ar.server.base.config;

import com.gxar.quick.ar.server.base.common.constant.WechatPayConstant;
import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.service.payments.jsapi.JsapiService;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author nice
 * @version 1.0
 * @created 2023/11/11.
 */
@Configuration
@RequiredArgsConstructor
public class WechatPaySdkConfig {

    public static Config getConfig() {
        Config config = new RSAAutoCertificateConfig.Builder().merchantId(WechatPayConstant.MERCHANT_ID)
                .privateKeyFromPath(WechatPayConstant.PRIVATE_KEY_PATH)
                .merchantSerialNumber(WechatPayConstant.MERCHANT_SERIAL_NUMBER).apiV3Key(WechatPayConstant.API_V3_KEY)
                .build();
        return config;
    }

    @Bean
    public JsapiService jsapiService() {
        // 使用自动更新平台证书的RSA配置
        // 一个商户号只能初始化一个配置，否则会因为重复的下载任务报错
        Config config = getConfig();
        // 构建service
        return new JsapiService.Builder().config(config).build();
    }
}
